using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components
{
    [Serializable]
    public class GlobalVariableSetterOutput:ResultBase
    {
        private List<GlobalVariable> _GlobalVars;

        public List<GlobalVariable> GlobalVars
        {
            get { return _GlobalVars; }
            set { _GlobalVars = value; }
        }

        public GlobalVariableSetterOutput()
            :base(Guid.Empty,Guid.Empty)
        {
            this._GlobalVars = null;
        }

        public GlobalVariableSetterOutput(Guid componentID, Guid jobTicketID, 
            List<GlobalVariable> gVars) 
            : base(componentID, jobTicketID)
        {
            this._GlobalVars = gVars;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            if (this.GlobalVars != null && this.GlobalVars.Count > 0)
            {
                XmlNode gVarRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "GlobalVariables");
                foreach (GlobalVariable gVar in this.GlobalVars)
                {
                    XmlNode gVarNode = XmlDataUtil.AddElement(ref xDoc, gVarRootNode, "GlobalVariable");
                    XmlDataUtil.UpdateAttribute(ref xDoc, gVarNode, "VariableName", gVar.VarName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, gVarNode, "DataType", ((int) gVar.DataType).ToString());
                    XmlDataUtil.UpdateAttribute(ref xDoc, gVarNode, "VariableValue", gVar.VarValue);
                }
            }

            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                GlobalVariableSetterOutput output = new GlobalVariableSetterOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.GlobalVars = null;
                XmlNodeList varNodes = xNode.SelectNodes("GlobalVariables/GlobalVariable");
                if (varNodes != null && varNodes.Count > 0)
                {
                    output.GlobalVars = new List<GlobalVariable>();
                    foreach (XmlNode varNode in varNodes)
                    {
                        GlobalVariable gVar = new GlobalVariable();
                        gVar.VarName = varNode.Attributes.GetNamedItem("VariableName").Value;
                        gVar.VarValue = varNode.Attributes.GetNamedItem("VariableValue").Value;
                        gVar.DataType =
                            (SimpleDataType) int.Parse(varNode.Attributes.GetNamedItem("DataType").Value);
                        output.GlobalVars.Add(gVar);
                    }
                }

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override Dictionary<string, Type> GetBindableMemberNames(Type[] excludeMemberFromTypes)
        {
            Dictionary<string, Type> bindableMemberNames = base.GetBindableMemberNames(excludeMemberFromTypes);
            if (bindableMemberNames == null)
            {
                bindableMemberNames = new Dictionary<string, Type>();
            }
            if (this.GlobalVars != null && this.GlobalVars.Count > 0)
            {
                foreach (GlobalVariable gVar in this.GlobalVars)
                {
                    bindableMemberNames.Add(gVar.VarName, SimpleDataTypeUtil.ToSystemType(gVar.DataType));
                }
            }
            return bindableMemberNames;
        }

        public override object ReadMemberValue(string memberName)
        {
            if (this._GlobalVars != null && this._GlobalVars.Count > 0)
            {
                foreach (GlobalVariable gVar in this._GlobalVars)
                {
                    if (gVar.VarName == memberName)
                        return SimpleDataTypeUtil.ReadDbValue(gVar.DataType, gVar.VarValue);
                }
            }
            return base.ReadMemberValue(memberName);
        }
    }
}
